package bz.ziro.kanbe.controller.mng.page;

import java.util.List;
import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import bz.ziro.kanbe.dao.PageDao;
import bz.ziro.kanbe.dao.SiteDao;
import bz.ziro.kanbe.dao.TemplateDao;
import bz.ziro.kanbe.model.Page;
import bz.ziro.kanbe.model.Site;
import bz.ziro.kanbe.model.Template;

/**
 * ページの検索
 * @author Administrator
 */
public class FindController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(FindController.class.getName());

    @Override
    public Navigation run() {

        String siteKeyBuf = (String)sessionScope("siteKey");
        Long siteKey = Long.valueOf(siteKeyBuf);

        //サイトを検索
        String keyBuf = param("pageKey");
        Page page = null;
        if ( keyBuf == null || keyBuf.equals("") ) {
        	Site site = SiteDao.find(siteKey);
            page = PageDao.findRootPage(site);
        } else {
            page = PageDao.find(siteKey,Long.valueOf(keyBuf));
        }

        requestScope("page",page);
        //サイト用のテンプレートをすべて検索
        List<Template> templateList = TemplateDao.all(siteKey);
        requestScope("templateList",templateList);

        return forward("page.jsp");
    }
}
